package com.itheima.edu.info.manager.dao;

import com.itheima.edu.info.manager.domain.Student;

import java.util.ArrayList;

public class OtherStudentDao implements BaseStudentDao{
    //1.创建集合容器,用于装数据
    public static ArrayList<Student> arr = new ArrayList<>();


    static{
        Student stu1 = new Student("heima001","张三","23","1997-11-11");
        Student stu2 = new Student("heima002","李四","24","1998-12-12");
        arr.add(stu1);
        arr.add(stu2);
    }

    //添加学生的方法
    @Override
    public boolean addSTtudent(Student student) {
        arr.add(student);
        return true;
    }

    //查看学生的方法
    @Override
    public Student[] findAllStudent() {

        //创建一个数组,用于将查到的数据存储到该数组中
        Student[] stu = new Student[arr.size()];
        //从集合中取出来,存一个,放一个
        for (int i = 0; i < arr.size(); i++) {
            stu[i] = arr.get(i);
        }


        //将数组返回
        return stu;
    }

    //删除方法
    @Override
    public void deleteStudentByid(String id) {
        //1.查找id在数组中对象的id相同的对象索引
        int index = getIndex(id);
        //2.使用集合中的remove(int index)方法解决
        arr.remove(index);
    }

    @Override
    public int getIndex(String id){
        //1.假设数组中的对象id与输入的id不符
        int index = -1;
        //2.得出id所在对象的索引
        for (int i = 0; i < arr.size(); i++) {
            Student stus = arr.get(i);
            if(stus != null && stus.getId().equals(id)){
                index = i;
                break;
            }
        }
        //3.返回索引
        return index;
    }

    @Override
    public void updateDao(String updateId, Student newStu) {
        //1.查找id在数组中对象的id相同的对象索引
        int index = getIndex(updateId);
        //2.修改对象
        arr.set(index,newStu);
    }
}
